'''
Created on 09-07-10

@author: peterd
'''




from web.service.LBCParser import LBCParser
from geo.models import NoSuchCity, NoSuchStreet, NoSuchLocation
from infra.logging import logger


class OtpParser(LBCParser):
	'''
	classdocs
	'''

	def __init__(self, lbcategory, dataProcessor): 
		LBCParser.__init__(self, lbcategory, dataProcessor)

		self.done_ = False
		self.inAtmTable_ = False
		self.fieldIdx_ = 0
				
		log = logger.getLogger()
		log.debug("%s = %s" % (self, self.__dict__))
		
		
	def reset__(self):
		self.fieldIdx_ = 0
		
		LBCParser.reset__(self)



	def start_tbody(self, attrs):
		if not self.done_:
			self.inAtmTable_ = True


	def start_td(self, attrs):
		if self.inAtmTable_:
			self.fieldIdx_ += 1

			
	def do_handle_data(self, data):
		log = logger.getLogger()
		
		if self.inAtmTable_:
			if self.fieldIdx_ == 1 and data != 'Mesto':
				try:
					self.setCity(data)
				except NoSuchCity, e:
					log.error('NoSuchCity exception caught, reason: %s' % e)
					
			if self.fieldIdx_ == 2 and data != 'Adresa':
				try:
					self.setStreet(data)
				except NoSuchStreet, e:
					log.error('NoSuchStreet exception caught, reason: %s' % e)

			if self.fieldIdx_ == 3:
				if data != 'Budova':
					try:
						if len(self.errors_) == 0:
							self.building_ = data
							self.createOrganization(name = 'OTP Banka Slovensko, a.s.', \
								            	url = 'http://www.otpbank.sk', \
								            	phone = '0850 111 222', \
												logo = 'icons/lbc/otpbanka.png', \
												email = '')
					except NoSuchLocation, e:
						log.error('NoSuchStreet exception caught, reason: %s' % e)

				self.reset__()

						
	def end_tbody(self):
		self.inAtmTable_ = False
		self.done_ = True


	def getId(self):
		return self.__class__.__name__